<template>
  <div>
    <div class="content">
      <h2>基本信息</h2>
      <el-form ref="form" :model="user" label-width="80px" class="formList">
        <el-form-item label="id" v-if="false">
          <el-input v-model="user.userId" />
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="user.username" readonly />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="user.nickname" />
        </el-form-item>
        <el-form-item label="密码">
          <el-button type="primary" @click="toUpdatePwd">点击修改</el-button>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="user.phoneNumber" />
        </el-form-item>
        <el-form-item label="头像">
          <el-upload
            action="api/upload"
            :headers="headers"
            :on-success="handlePreview"
            multiple
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传头像</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="user.sex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" class="subBtn">保存</el-button>
        </el-form-item>
      </el-form>

      <!-- 修改弹窗 -->
      <el-dialog title="修改" :visible.sync="updateDialog">
        <admin-update-pwd @getPwd="getPwd" @closeUpdateDialog="closeUpdateDialog" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import userApi from "@/api/user";
import AdminUpdatePwd from "./updatePwd";
import { Message } from "element-ui";
export default {
  name: "Mine",
  components: {
    AdminUpdatePwd
  },
  data() {
    return {
      headers: {
        //上传文件的请求头
        Authorization: this.$store.state.token
      },
      updateDialog: false, // 修改密码弹窗
      uploadUrl: "",
      user: {
        userId: "",
        username: "",
        nickname: "",
        phoneNumber: "",
        password: "",
        sex: 1,
        avt: ""
      },
      avt: ""
    };
  },
  watch: {
    // 监听器这里的function不能使用箭头函数替代
    "adminUser.header": function(newVal, oldVal) {
      this.avt = newVal;
    }
  },
  created() {
    this.user = this.$store.state.user;
    this.user.password = "";
  },
  methods: {
    getPwd(data) {
      this.user.password = data;
      this.updateDialog = false;
    },
    handlePreview(response, file, fileList) {
      this.user.avt = response.data.ulr;
    },
    onSubmit() {
      let this_ = this;
      userApi.update(this_.user).then(res => {
        if (this_.user.password != "") {
          this_.$store.commit("SET_TOKEN", "");
          this_.$router.replace("/");
        }
        Message.success(res.message)
      });
    },
    toUpdatePwd() {
      this.updateDialog = true;
    },
    closeUpdateDialog() {
      this.updateDialog = false;
    }
  }
};
</script>
<style scoped lang="less">
.content {
  width: 70%;
  padding: 10px 20px;
  margin: 0 auto;
  // background: red;
}
.formList {
  background: #fff;
  padding: 10px 20px;
}
.el-input {
  width: 60%;
}
.el-textarea {
  width: 60%;
}

/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: 1px solid #409eff;
}
/deep/ .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
/deep/ .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
